package com.md.application.assembler;

import com.md.base.exception.ServerException;
import com.md.domain.model.bo.QueryApiBO;
import com.md.domain.model.po.ApiPO;
import com.md.infrastructure.value.ErrorEnum;
import com.md.request.ApiRequest;
import com.md.response.ApiResponse;
import com.md.util.ClassUtils;
import com.md.util.ObjectUtil;
import com.md.value.ApiTypeEnum;
import com.md.value.enums.ApiMethodEnum;
import com.md.value.enums.AuthEnum;
import com.md.value.enums.OperationEnum;
import lombok.experimental.UtilityClass;

import java.util.List;
import java.util.Optional;

/**
 * @author 刘骄阳
 * @date 2024/7/14 22:22
 **/
@UtilityClass
public class ApiAssembler {
  public static ApiPO buildApiRequest2PO(final ApiRequest request) {
    return ClassUtils.source2Target(request, ApiPO.class, target -> {
      target.setApiType(request.getApiType().getCode());
      target.setMethod(request.getMethod().getCode());
      target.setAuth(request.getAuth().getCode());
      target.setOperation(request.getOperation().getCode());
    });
  }

  public static List<ApiResponse> buildApiPO2Response(final List<ApiPO> list) {
    return ClassUtils.source2Target(list, ApiResponse.class, (source, target) -> {
      target.setApiType(ApiTypeEnum.of(source.getApiType()));
      target.setMethod(ApiMethodEnum.of(source.getMethod()));
      target.setAuth(AuthEnum.of(source.getAuth()));
      target.setOperation(OperationEnum.of(source.getOperation()));
    });
  }

  public static ApiResponse buildApiPO2Response(final ApiPO api) {
    return ClassUtils.source2Target(api, ApiResponse.class, apiResponse -> {
      apiResponse.setApiType(ApiTypeEnum.of(api.getApiType()));
      apiResponse.setMethod(ApiMethodEnum.of(api.getMethod()));
      apiResponse.setAuth(AuthEnum.of(api.getAuth()));
      apiResponse.setOperation(OperationEnum.of(api.getOperation()));
    });
  }

  public static ApiPO buildApiPO(final QueryApiBO queryBO) {
    return ClassUtils.source2Target(queryBO, ApiPO.class);
  }

  public static QueryApiBO buildQueryApiBO(final ApiRequest request) {
    return ClassUtils.source2Target(request, QueryApiBO.class, queryApiBO -> {
      Optional.ofNullable(request.getMethod()).map(ApiMethodEnum::getCode).ifPresent(queryApiBO::setMethod);
      Optional.ofNullable(request.getApiType()).map(ApiTypeEnum::getCode).ifPresent(queryApiBO::setApiType);
      Optional.ofNullable(request.getAuth()).map(AuthEnum::getCode).ifPresent(queryApiBO::setAuth);
    });
  }

  public static void checkParam(final ApiRequest request) {
    if (ObjectUtil.isEmpty(request.getMethod())) {
      throw ServerException.build(ErrorEnum.PARAM_IS_ALL_EMPTY);
    }
    if (ObjectUtil.isEmpty(request.getAuth())) {
      throw ServerException.build(ErrorEnum.PARAM_IS_ALL_EMPTY);
    }
    if (ObjectUtil.isEmpty(request.getOperation())) {
      throw ServerException.build(ErrorEnum.PARAM_IS_ALL_EMPTY);
    }
    if (ObjectUtil.isEmpty(request.getUrl())) {
      throw ServerException.build(ErrorEnum.PARAM_IS_ALL_EMPTY);
    }
    if (ObjectUtil.isEmpty(request.getAppInfoId())) {
      throw ServerException.build(ErrorEnum.PARAM_IS_ALL_EMPTY);
    }
    if (ObjectUtil.isEmpty(request.getTitle())) {
      throw ServerException.build(ErrorEnum.PARAM_IS_ALL_EMPTY);
    }
  }
}
